package com.cloudfun.campusshare.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

/**
 * @author : zhangyongjie
 * @version : 1.0
 * @createTime : 2020/4/20 20:35
 * @description :
 */
@Component
public class OrderNoGenUtil {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /**上一秒**/
    private static long lastTime = Instant.now().toEpochMilli();

    private static LocalDate lastDate = LocalDate.now();

    public String next0(String suffix,String type){
        StringBuffer sb = new StringBuffer();
        sb.append(suffix);
        long now = Instant.now().toEpochMilli();
        LocalDateTime l = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        sb.append(formatter.format(l));
        if(now - lastTime > 1000){
            lastTime = now;
            stringRedisTemplate.delete(type);
        }
        Long orderSuffix = stringRedisTemplate.opsForValue().increment(type, 1);
        if(orderSuffix < 10){
            sb.append("000").append(orderSuffix);
        }else if(orderSuffix < 100){
            sb.append("00").append(orderSuffix);
        }else if(orderSuffix < 1000){
            sb.append("0").append(orderSuffix);
        }else {
            sb.append(orderSuffix);
        }
        return sb.toString();
    }

    public String next(){
        return next0("OD","ORDER_SUFFIX");
    }

    public String next4WorkerOrder(){
        StringBuffer sb = new StringBuffer();
        sb.append("WD");
        long now = Instant.now().toEpochMilli();
        LocalDateTime l = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        sb.append(formatter.format(l));
        if(now - lastTime > 1000){
            lastTime = now;
            stringRedisTemplate.delete("WORKER_ORDER_SUFFIX");
        }
        Long orderSuffix = stringRedisTemplate.opsForValue().increment("WORKER_ORDER_SUFFIX", 1);
        if(orderSuffix < 10){
            sb.append("000").append(orderSuffix);
        }else if(orderSuffix < 100){
            sb.append("00").append(orderSuffix);
        }else if(orderSuffix < 1000){
            sb.append("0").append(orderSuffix);
        }else {
            sb.append(orderSuffix);
        }
        return sb.toString();
    }

    public String next4Advance(String suffix,String type){
        StringBuffer sb = new StringBuffer();
        sb.append(suffix);
        LocalDate now = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        sb.append(formatter.format(now));
        if(lastDate.until(now, ChronoUnit.DAYS) > 0){
            lastDate = now;
            stringRedisTemplate.delete(type);
        }
        Long orderSuffix = stringRedisTemplate.opsForValue().increment(type, 1);
        if(orderSuffix < 10){
            sb.append("00000").append(orderSuffix);
        }else if(orderSuffix < 100){
            sb.append("0000").append(orderSuffix);
        }else if(orderSuffix < 1000){
            sb.append("000").append(orderSuffix);
        }else if(orderSuffix < 10000){
            sb.append("00").append(orderSuffix);
        }else if(orderSuffix < 100000){
            sb.append("0").append(orderSuffix);
        }else {
            sb.append(orderSuffix);
        }
        return sb.toString();
    }

}
